package model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class Parser {
	private Estrategia estrategia;
	//
	private int cantPk;

	public Parser() {

	}

	public Estrategia getEstrategia() {
		return estrategia;
	}

	public void setEstrategia(Estrategia estrategia) {
		this.estrategia = estrategia;
	}

	public int pid(int value, int value2) {
		return ((value & 0x001F) << 8) | (value2 & 0x00FF);
	}

	public Packet retornarPacket(FileInputStream file) throws IOException {

		Packet p = new Packet();

		p.setSync(file.read());
		int value = file.read();
		p.setPid(pid(value, file.read()));
		p.setError(value & 0x80);
		p.setConf(file.read());
		guardarResto(file, p);

		return p;
	}

	private void guardarResto(FileInputStream file, Packet p) {
		for (int i = 0; i < 184; i++) {
			try {
				p.getPayload()[i] = (byte) (file.read() & 0x00FF);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void parser(FileInputStream file) {

		try {
			while (file.available() > 0) {
				this.getEstrategia().actuar(retornarPacket(file));
			}
		} catch (IOException e) {
			// Fin de Archivo
		}
	}
	public int getCantPk() {
		return cantPk;
	}

	public void setCantPk(int cantPk) {
		this.cantPk = cantPk;
	}
}
